#ifndef __RPC_TRANSMISSION_TCP_SERVER_H__
#define __RPC_TRANSMISSION_TCP_SERVER_H__

#include "ret.h"
#include "message.h"

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <unistd.h>

extern char g_server_run_flag;

#define EPOLL_MAX_EVENTS (1024)
#define TCP_RECV_BUFFER_SIZE (1024 * 8)

typedef struct _tcp_config
{
    int port_min;
    int port_max;
    int backlog;
    int recv_queue_max_size;
}tcp_server_config_t;

typedef struct _tcp_server
{
    tcp_server_config_t* config;
    int server_fd; // 操作句柄
    int epoll_fd; // 复用句柄
    struct sockaddr_in server_addr;
    pthread_t accept_thread;
    struct epoll_event events[EPOLL_MAX_EVENTS];
    message_queue_t* recv_queue;
    pthread_cond_t flag_to_received_msg;
    pthread_mutex_t mutex_to_received_msg;
}tcp_server_t;

tcp_server_t* tcp_server_create(tcp_server_config_t* config);
ret_t tcp_server_start(tcp_server_t* server);
ret_t tcp_server_notify(tcp_server_t* server);

void tcp_server_destroy(tcp_server_t** server);

#endif // __RPC_TRANSMISSION_TCP_SERVER_H__